package com.etlmaster.executor.steps.loop;

import java.util.concurrent.ExecutionException;

import com.etlmaster.executor.EtlMasterException;
import com.etlmaster.executor.Job;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.utils.DpExecutorUtils;

/**
 * @author Wugf
 * @date 2016-03-08
 */
public abstract class LoopProcessor {

	protected LoopStepMeta loopStepMeta;
	protected Job job;
	protected String expression;
	protected LoopStep loopStep;
	protected int i = 0;

	public void init(StepMeta curStepMeta, Job job) {
		loopStepMeta = (LoopStepMeta) curStepMeta;
		this.job = job;
		loopStep = (LoopStep) loopStepMeta.getStep();
		expression = DpExecutorUtils.variableSubstitution(
				loopStepMeta.getExpression(), job.getVariables());
		expression = DpExecutorUtils.replaceMultiSpaceToOneSpace(expression)
				.trim();
	}

	public abstract int process() throws InterruptedException, ExecutionException;

	public int loop(StepMeta curStepMeta, Job job) {
		init(curStepMeta, job);
		try {
			return process();
		} catch (InterruptedException e) {
			throw new EtlMasterException(e.getMessage());
		} catch (ExecutionException e) {
			throw new EtlMasterException(e.getMessage());
		}

	}


	

}
